import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import employees from './modules/employees'
import departments from './modules/departments'
import approvals from './modules/approvals'
import attendances from './modules/attendances'
import permission from './modules/permission'
import salarys from './modules/salarys'
import setting from './modules/setting'
import social from './modules/social'

// import employees from '@/views/employees'

// 动态路由
// 由权限决定用户能看到的

export const asyncRoutes = [
  // {
  //   path: '/employees',
  //   component: employees
  // },
  // {
  //   path: '/employees',
  //   component: Layout,
  //   // redirect: '/dashboard',
  //   children: [{
  //     // children 里的path为空 当前这个路由回座位一级渲染的默认路由
  //     path: '',
  //     name: 'employees',
  //     component: () => import('@/views/employees'),
  //     meta: { title: 'Dashboard', icon: 'dashboard' }
  //   }]
  // }
  departments,
  setting,
  employees,
  permission,
  approvals,
  attendances,
  salarys,
  social
]

// 静态路由
// 用户都可以访问

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    // 隐藏路由显示
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      // path: '',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    path: '/uploadExcel',
    component: Layout,
    // 隐藏导入模块 通过点击导入按钮调用路由页面
    hidden: true, // 不显示到左侧 不参与遍历
    children: [
      {
        path: '',
        name: 'uploadExcel',
        component: () => import('@/views/uploadExcel'),
        meta: { title: '导入', icon: 'dashboard' }
      }
    ]
  }
]

// {
//   path: 'external-link',
//   component: Layout,
//   children: [
//     {
//       path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
//       meta: { title: 'External Link', icon: 'link' }
//     }
//   ]
// },

// 404 page must be placed at the end !!!
// { path: '*', redirect: '/404', hidden: true }
// ]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  // 管理滚动行为 不同页面之间的滚动行为互不干扰 切换后页面回到顶部
  scrollBehavior: () => ({ y: 0 }),
  // routes: constantRoutes
  // 动 + 静 结合
  // 这里的前后顺序可以改变页面显示的顺序
  // routes: [...constantRoutes, ...asyncRoutes, { path: '*', redirect: '/404', hidden: true }]
  routes: [...constantRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  // 重置路由
  // 退出登录时把之前的路由记录清除掉
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
